import ConfigParser, os, sys
from libs.config_variables import config_variables

class read_config(config_variables):
#	''' Configuration files '''
#	__config_files = ['/etc/zabbix_notify.ini', os.path.expanduser('~/.zabbix_notify.ini'), './zabbix_notify.ini']
	
	''' Configuration sections '''
	__sections = ['server', 'settings', 'sounds', 'icons', 'alarms'] 
	
	def __init__(self, config_files = ['/etc/zabbix_notify.ini', os.path.expanduser('~/.zabbix_notify.ini'), './zabbix_notify.ini']):
		''' Inherit config variables '''
		config_variables.__init__(self)
		self.__config_files = config_files
		self.config = ConfigParser.ConfigParser()
		self.read()
		
	def read(self):
		try:
			self.config.read(self.__config_files)
		except Exception as e:
			print "Error reading config file:\n", str(e)
			raise
		
		''' Read Zabbix server section '''
		try:
			self.set_zabbixserver_hostname(self.config.get(self.__sections[0], 'hostname'))
			self.set_zabbixserver_username(self.config.get(self.__sections[0], 'username'))
			self.set_zabbixserver_password(self.config.get(self.__sections[0], 'password'))
		except Exception as e:
			print "Error reading config file (section: \'" + self.__sections[0] + "\'):\n", str(e)
			raise

		''' Read Zabbix Notify Settings '''
		try:
			self.set_logfile(self.config.get(self.__sections[1], 'logfile'))
			self.set_debug(self.config.getboolean(self.__sections[1], 'debug'))
			self.set_verbose(self.config.getboolean(self.__sections[1], 'verbose'))
			self.set_update_interval(self.config.getint(self.__sections[1], 'updateinterval'))
			self.set_blink_interval(self.config.getint(self.__sections[1], 'blinkinterval'))
			self.set_icons_path(self.config.get(self.__sections[1], 'icons_path'))
			self.set_sounds_path(self.config.get(self.__sections[1], 'sounds_path'))
		except Exception as e:
			print "Error reading config file (section: \'" + self.__sections[1] + "\'):\n", str(e)
			raise

		''' Read sounds '''
		try:
			self.set_sound_off(os.path.join(self.get_sounds_path(), self.config.get(self.__sections[2], 'off')))
			self.set_sound_notclassified(os.path.join(self.get_sounds_path(), self.config.get(self.__sections[2], 'notclassified')))
			self.set_sound_info(os.path.join(self.get_sounds_path(), self.config.get(self.__sections[2], 'info')))
			self.set_sound_warning(os.path.join(self.get_sounds_path(), self.config.get(self.__sections[2], 'warning')))
			self.set_sound_average(os.path.join(self.get_sounds_path(), self.config.get(self.__sections[2], 'average')))
			self.set_sound_high(os.path.join(self.get_sounds_path(), self.config.get(self.__sections[2], 'high')))
			self.set_sound_disaster(os.path.join(self.get_sounds_path(), self.config.get(self.__sections[2], 'disaster')))
		except Exception as e:
			print "Error reading config file (section: \'" + self.__sections[2] + "\'):\n", str(e)
			raise
		
		''' Read icons '''
		try:
			self.set_icon_off(os.path.join(self.get_icons_path(), self.config.get(self.__sections[3], 'off')))
			self.set_icon_notclassified(os.path.join(self.get_icons_path(), self.config.get(self.__sections[3], 'notclassified')))
			self.set_icon_info(os.path.join(self.get_icons_path(), self.config.get(self.__sections[3], 'info')))
			self.set_icon_warning(os.path.join(self.get_icons_path(), self.config.get(self.__sections[3], 'warning')))
			self.set_icon_average(os.path.join(self.get_icons_path(), self.config.get(self.__sections[3], 'average')))
			self.set_icon_high(os.path.join(self.get_icons_path(), self.config.get(self.__sections[3], 'high')))
			self.set_icon_disaster(os.path.join(self.get_icons_path(), self.config.get(self.__sections[3], 'disaster')))
		except Exception as e:
			print "Error reading config file (section: \'" + self.__sections[3] + "\'):\n", str(e)
			raise
		
		try:
			self.set_min_severity_sound(self.config.get(self.__sections[4], 'min_severity_sound'))
			self.set_min_severity_blink(self.config.get(self.__sections[4], 'min_severity_blink'))
		except Exception as e:
			print "Error reading config file (section: \'" + self.__sections[4] + "\'):\n", str(e)
			raise
